﻿using System;
using System.IO;
using System.Net;

namespace Communication.TcpIpService
{
    public class FtpService
    {
        private string ftpServerIP;
        private string ftpRemotePath;
        private string ftpUserID;
        private string ftpPassword;
        private string ftpURI;

        /// <summary>
        /// 连接FTP
        /// </summary>
        /// <param name="FtpServerIP">FTP连接地址</param>
        /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
        /// <param name="FtpUserID">用户名</param>
        /// <param name="FtpPassword">密码</param>
        public FtpService(string ServerIP, string RemotePath, string UserID, string Password)
        {
            ftpServerIP = ServerIP;
            ftpRemotePath = RemotePath;
            ftpUserID = UserID;
            ftpPassword = Password;
            ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
        }
<<<<<<< HEAD
        public long GetFileSize(string Filename)
        {
            FileInfo fileInfo = new FileInfo(Filename); //实例化传入的配置文件信息
            string filename = fileInfo.Name;  //提取文件名
            FtpWebRequest reqFTP;
            long fileSize = 0;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                fileSize = response.ContentLength;

                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("FtpHelper GetFileSize Error --> " + ex.Message);
            }
            return fileSize;
        }
        public void Delete(string FileName)
        {

            try
            {
                FileInfo fileInfo = new FileInfo(FileName); //实例化传入的配置文件信息
                string filename = fileInfo.Name;  //提取文件名
                string uri = ftpURI + filename;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
                reqFTP.UsePassive = false;

                string result = String.Empty;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                long size = response.ContentLength;
                Stream datastream = response.GetResponseStream();
                StreamReader sr = new StreamReader(datastream);
                result = sr.ReadToEnd();
                sr.Close();
                datastream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("FtpHelper Delete Error --> " + ex.Message + "  文件名:");
            }
        }
=======
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="filename">文件名</param>
        public void UploadFile(string FileName)
        {
<<<<<<< HEAD
            FileInfo fileInfo = new FileInfo(FileName);
            string uri = ftpURI + fileInfo.Name;
            FtpWebRequest reqFTP;

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
=======
            FileInfo fileInfo = new FileInfo(FileName); //实例化传入的配置文件信息
            string uri = ftpURI + fileInfo.Name;  //提取文件名，加入到上传路径之后
            FtpWebRequest reqFTP;  //实现文件传输的ftp客户端

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            reqFTP.KeepAlive = false;
            reqFTP.UseBinary = true;
            //reqFTP.UsePassive = false;
            reqFTP.Proxy = null;

            reqFTP.ContentLength = fileInfo.Length;
            int BufferSize = 2048;
            byte[] buff = new byte[BufferSize];
            int ContentLength;
            FileStream fs = fileInfo.OpenRead();

            try
            {
                Stream strm = reqFTP.GetRequestStream();
                ContentLength = fs.Read(buff, 0, BufferSize);
                while (ContentLength != 0)
                {
                    strm.Write(buff, 0, ContentLength);
                    ContentLength = fs.Read(buff, 0, BufferSize);
                }

                strm.Close();
                fs.Close();
                //MessageBox.Show("配置成功，请重启下位机");
            }
            catch (Exception ex)
            {
                //MessageBox.Show("下传配置失败" + ex.ToString());

                //throw new Exception("Ftphelper Upload Error --> " + ex.Message);
            }
        }
<<<<<<< HEAD
=======

        public void DownLoadFile(string filePath, string fileName)
        {

            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(filePath + "/" + fileName, FileMode.Create);

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("FtpHelper Download Error --> " + ex.Message);
            }
        }

        public long GetFileSize(string Filename)
        {
            FileInfo fileInfo = new FileInfo(Filename); //实例化传入的配置文件信息
            string filename = fileInfo.Name;  //提取文件名
            FtpWebRequest reqFTP;
            long fileSize = 0;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                fileSize = response.ContentLength;

                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("FtpHelper GetFileSize Error --> " + ex.Message);
            }
            return fileSize;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName"></param>
        public void Delete(string FileName)
        {
           
            try
            {
                FileInfo fileInfo = new FileInfo(FileName); //实例化传入的配置文件信息
                string filename = fileInfo.Name;  //提取文件名
                string uri = ftpURI + filename;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
                reqFTP.UsePassive = false;

                string result = String.Empty;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                long size = response.ContentLength;
                Stream datastream = response.GetResponseStream();
                StreamReader sr = new StreamReader(datastream);
                result = sr.ReadToEnd();
                sr.Close();
                datastream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("FtpHelper Delete Error --> " + ex.Message + "  文件名:");
            }
        }
>>>>>>> b19c74022a89f58c8c960e2031f2461983bbb895
    }
}
